package com.hy.controller;

import com.hy.domain.UserFriend;
import com.hy.domain.dto.R;
import com.hy.domain.dto.UserDTO;
import com.hy.service.UserFriendService;
import com.hy.util.UserHolder;
import com.hy.util.WebsocketUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

@RestController
@RequestMapping("user_friends")
public class UserFriendsController {
    @Resource
    private UserFriendService userFriendService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("list")
    public R listFirends() {

        final Long userId = UserHolder.getUser().getUserId();
        String key =  "user:firend:" + userId;
        List<UserDTO> friendList = userFriendService.listFriendsByUserId(userId);
        // 设置在线状态
        return R.ok(friendList);
    }

    /**
     * TODO 添加好友，将添加好友的信息存放在redis/db/mq中，被添加对象登陆后通过申请才添加成功
     * @param friendId
     * @param message
     * @return
     */
    @PostMapping
    public R add(@Param("friendId") Long friendId, @Param("message") String message) {
        System.out.println("friendId = " + friendId);
        final Long userId = UserHolder.getUser().getUserId();
        userFriendService.addFriend(friendId);
        return R.ok().message("添加成功");
    }

    @DeleteMapping("/{friendId}")
    public R delete(@PathVariable Long friendId) {
        final Long userId = UserHolder.getUser().getUserId();
        userFriendService.deleteFriend(friendId);
        return R.ok().message("删除成功");
    }

    @GetMapping("/{keyword}")
    public R search(@PathVariable String keyword) {
        List<UserFriend> list = userFriendService.searchByKeyword(keyword.trim());
        return R.ok(list);
    }

}
